package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.util.JSONs;
import com.atguigu.gmall.product.vo.ValueSkuJson;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author lfy
* @description 针对表【spu_sale_attr(spu销售属性)】的数据库操作Service实现
* @createDate 2023-08-14 16:26:08
*/
@Service
public class SpuSaleAttrServiceImpl extends ServiceImpl<SpuSaleAttrMapper, SpuSaleAttr>
    implements SpuSaleAttrService{

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {


        return  baseMapper.getSpuSaleAttrList(spuId);
    }

    @Override
    public List<SpuSaleAttr> getSpuSaleAttrAndMarkSku(Long spuId, Long skuId) {
        return baseMapper.getSpuSaleAttrAndMarkSku(spuId,skuId);
    }

    @Override
    public String getValueSkuJson(Long spuId) {
         List<ValueSkuJson> jsons = baseMapper.getValueSkuJson(spuId);

        Map<String, Long> map = jsons.stream()
                .collect(Collectors.toMap(ValueSkuJson::getAttrValue, ValueSkuJson::getId));


        //fastjson
        String jsonStr = JSONs.toJSONStr(map);

        return jsonStr;
    }
}




